package org.enlongWu.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.enlongWu.mapper.UserMapper;
import org.enlongWu.pojo.Article;
import org.enlongWu.pojo.PageBean;
import org.enlongWu.pojo.Role;
import org.enlongWu.pojo.User;
import org.enlongWu.service.UserService;
import org.enlongWu.utils.Md5Util;
import org.enlongWu.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    /**
     *  查找
     * @param username
     * @return
     */
    @Override
    public User findByUserName(String username) {
        User u = userMapper.findByUserName(username);
        return u;
    }

    @Override
    public User findByUserid(Integer id) {
        return userMapper.findByUserId(id);
    }

    /**
     * 注册
     * @param username
     * @param password
     */
    @Override
    public void register(String username, String password,String nickname,String email,String userPic,String role) {
        //加密处理再存入
        String md5String = Md5Util.getMD5String(password);
        //添加
        userMapper.add(username,md5String,nickname,email,userPic,role);
    }

    /**
     * 更新用户信息
     * @param user
     */
    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd, Integer id) {
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }


    /**
     * 用于分页查询文章列表
     * 首先创建一个PageBean对象，然后使用PageHelper插件进行分页查询。
     * 接着调用mapper接口的list方法获取文章列表，并将结果封装到PageBean对象中返回。
     * @param pageNum 当前页码
     * @param pageSize 每页显示的记录数
     * @param role 权限类型
     * @return  用户的分页对象
     */
    @Override
    public PageBean<User> list(Integer pageNum, Integer pageSize,String searchString ,String role) {
        //1.创建PageBean对象
        PageBean<User> pb = new PageBean<>();

        //2.开启分页查询 PageHelper
        PageHelper.startPage(pageNum,pageSize);

        //3.调用mapper
        List<User> as = userMapper.list(searchString,role);//查询到的用户列表
        //Page中提供了方法,可以获取PageHelper分页查询后 得到的总记录条数和当前页数据
        Page<User> p = (Page<User>) as;

        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public List<User> getUserListByRoleList(List<Role> roleList) {
        List<User> userList = new ArrayList<>();
        for (Role role : roleList) {
            System.out.println(role);
            userList.addAll(userMapper.getUserListByRole(role.getRole()));
        }
        return userList;
    }


}
